<template>
  <div class="con_store_main">
    <!--新增窗口-->
    <el-dialog title="发布消息通知" :visible.sync="addBox" width="50%" :close-on-click-modal="true" center>
      <div>
        <el-form :model="addForm" :rules="rules">
          <el-form-item label="消息通知标题" prop="title">
            <el-input maxlength="12" show-word-limit v-model="addForm.title" placeholder="输入消息通知标题"></el-input>
          </el-form-item>

          <el-form-item label="消息通知类型" prop="messageType">
            <el-select v-model="addForm.messageType" placeholder="选择消息通知类型">
              <el-option v-for="item in types" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="消息通知内容" prop="content">
            <el-input maxlength="30" show-word-limit type="textarea" v-model="addForm.content"
              placeholder="输入消息通知的内容"></el-input>
          </el-form-item>
          <div>
            <el-button @click="confirmAdd">確認添加</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
    <!--编辑窗口-->
    <el-dialog title="修改信息通知" :visible.sync="updateBox" width="50%" :close-on-click-modal="true" center>
      <div>
        <el-form :model="updateForm" :rules="rulesEdit">
          <el-form-item label="消息通知标题" prop="title">
            <el-input maxlength="12" show-word-limit v-model="updateForm.title" placeholder="输入消息通知标题"></el-input>
          </el-form-item>
          <el-form-item label="消息通知类型" prop="messageType">
            <el-select v-model="updateForm.messageType" placeholder="选择消息通知类型">
              <el-option v-for="item in types" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="消息通知内容" prop="content">
            <el-input maxlength="30" show-word-limit v-model="updateForm.content" placeholder="输入消息通知内容"></el-input>
          </el-form-item>
          <div>
            <el-button @click="confirmUpdate">確認修改</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="通知标题">
        <el-input v-model="formInline.title" placeholder="输入通知标题"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="findData" type="primary" icon="el-icon-search">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="addMessageToAll" type="primary" icon="el-icon-plus">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table class="table" :data="tableData" style="width: 100%" v-loading="loading"
      :default-sort="{ prop: 'index', order: 'descending' }">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="消息内容:">
              <span>{{ props.row.content }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="index" label="序号" sortable width="120">
      </el-table-column>
      <el-table-column prop="title" label="消息标题">
      </el-table-column>
      <el-table-column prop="messageType" label="消息类型">
      </el-table-column>
      <el-table-column prop="sendTime" label="发送时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="editMessage(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteMessage(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin: auto; margin-top: 8px" :hide-on-single-page="value" :current-page.sync="currentPage"
      :page-size="6" :pager-count="11" layout="prev, pager, next" :total="this.total"
      @current-change="handleSizeChange">
    </el-pagination>
  </div>
</template>
<script>
import { request } from "@/network/request/request";
export default {
  mounted() {
    this.findData();
  },
  data() {
    return {
      rules: {
        title: [
          { required: true, message: '请输入消息通知标题', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        messageType: [
          { required: true, message: '请选择消息类型', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入消息通知的内容', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
      },
      rulesEdit: {
        title: [
          { required: true, message: '请输入消息通知标题', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        messageType: [
          { required: true, message: '请选择消息类型', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入消息通知的内容', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
      },
      addForm: {
        account: localStorage.getItem("account"),
        title: '',
        messageType: '',
        content: '',
      },
      updateForm: {
        messageId: 0,
        title: '',
        messageType: '',
        content: '',
      },
      types: [{ value: '系统通知', label: '系统通知' }],
      formInline: { title: '' },
      updateBox: false,
      addBox: false,
      currentPage: 1, // 当前页码
      pageSize: 6, // 每页显示的记录数
      total: 0, // 总数目，默认设置为 0
      loading: false,
      value: false,
      tableData: [],

    };
  },
  methods: {
    time() {
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
      }, 600);
    },
    handleSizeChange() {
      this.findData();
    },
    deleteAlert(row) {
      this.$confirm('此操作将永久删除消息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest(row);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    editMessage(row) {
      console.log(row);
      this.updateForm.messageId = row.messageId;
      this.updateForm.title = row.title;
      this.updateForm.content = row.content;
      this.updateForm.messageType = row.messageType;
      this.updateBox = true;
    },
    addMessageToAll() {
      this.addBox = true;
    },
    deleteMessage(row) {
      this.deleteAlert(row);
    },
    async confirmUpdate() {
      const config = {
        url: 'api/admin/message',
        method: "put",
        data: {
          messageId: this.updateForm.messageId,
          title: this.updateForm.title,
          content: this.updateForm.content,
          messageType: this.updateForm.messageType,
        }
      }
      const res = await request(config);
      if (res.data.code === 1) {
        this.$message.success("修改成功");
        this.findData();
        this.updateBox = false;
      }
    },
    async findData() {
      this.time();
      const params = {
        title: this.formInline.title,
        account: localStorage.getItem("account"),
        currentPage: this.currentPage /* 当前页码 */,
        pageSize: this.pageSize /* 页码大小 */,
      };
      const config = {
        url: "api/admin/message",
        method: "get",
        params: params,
      };
      const res = await request(config);
      if (res.data.code === 1) {
        this.total = res.data.data.total; /* 得到数据的总数 */
        this.tableData = res.data.data.pageList; /* 得到数据 */
      }

    },
    async deleteRequest(row) {
      let messageId = row.messageId;
      const config = {
        url: `api/admin/message/${messageId}`,/* 通过路径参数 路径参数 */
        method: "delete",
      }
      const res = await request(config);
      if (res.data.code === 1) {
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.findData();
      } else {
        this.$message({
          type: 'error',
          message: '删除失败'
        });
      }

    },
    async confirmAdd() {
      const config = {
        url: "api/admin/message",
        method: "post",
        data: {
          account: localStorage.getItem("account"),
          title: this.addForm.title,
          messageType: this.addForm.messageType,
          content: this.addForm.content,
          status: this.addForm.status,
        }
      }
      const res = await request(config);
      if (res.data.code == 1) {
        this.$message.success("添加成功");
        this.addBox = false;
      }
      this.findData();
    },

  },
};
</script>
<style scoped>
.con_store_main {
  height: 498px;
  display: flex;
  flex-direction: column;
  align-content: center;
  justify-content: center;
}

.ic-status {
  display: flex;
  align-content: center;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  font-weight: bolder;
  color: #7c7c7d;
}
</style>